package org.seed.rpc.core.util;

public class Ipv4IntValConvertor {


    /**
     * IP地址转化为Integer类型的值
     */
    public static int ipv4ToInt(String ipv4Str) {
        String[] ipSlices = ipv4Str.split("\\.");
        int result = 0;

        for (int i = 0; i < ipSlices.length; i++) {
            int intSlice = Integer.parseInt(ipSlices[i]) << 8 * i;
            result = result | intSlice;
        }
        return result;
    }

    /**
     * Integer类型的值转化为IPV4地址
     */
    public static String intToIpv4(int ipv4Int) {
        String[] ipString = new String[4];
        for (int i = 0; i < 4; i++) {
            int pos = i * 8;
            int and = ipv4Int & (255 << pos);
            ipString[i] = String.valueOf(and >> pos);
        }
        return String.join(".", ipString);
    }


}
